package com.example.task1.ui.fastfood;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.base.BaseActivity;
import com.example.task1.bean.FastFoodDetailBean;
import com.example.task1.bean.FastFoodSearchBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.HashMap;

import retrofit2.Call;

public class FastFoodDetailActivity extends BaseActivity {

    private TextView tvName;
    private ImageView ivLike;
    private ImageView ivCollect;
    private TextView tvIngredients;
    private RecyclerView rvList;
    private int id;
    private FastFoodDetailAdapter fastFoodDetailAdapter;
    private FastFoodDetailBean.DataDTO rows;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fast_food_detail);
        id = getIntent().getIntExtra("id", 0);
        initView();
        initData();
        setAdapter();
        business();
    }

    private void business() {
        ivCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("dishesId", id);
                if (rows.isMyCollectionState()) {
                    map.put("newState", false);
                } else {
                    map.put("newState", true);
                }
                Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/kitchen-helper/dishes-collection", Api.getBody(map));
                Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                    @Override
                    public void success(ApiResp data) {
                        if (rows.isMyCollectionState()) {
                            ivCollect.setImageResource(R.drawable.ic_baseline_grade_24);
                        } else {
                            ivCollect.setImageResource(R.drawable.ic_baseline_grade_yellow_24);
                        }
                        MyApplication.show("收藏成功");
                    }

                    @Override
                    public void error(String msg) {

                    }
                });
            }
        });
        ivLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Object> map1 = new HashMap<>();
                map1.put("dishesId", id);
                if (rows.isMyLikeState()) {
                    map1.put("newState", false);
                } else {
                    map1.put("newState", true);
                }
                Call<ApiResp> post1 = Api.api().post(MyApplication.TOKEN, "/prod-api/api/kitchen-helper/dishes-like", Api.getBody(map1));
                Api.send(post1, ApiResp.class, new CallBack<ApiResp>() {
                    @Override
                    public void success(ApiResp data) {
                        if (rows.isMyLikeState()) {
                            ivLike.setImageResource(R.drawable.ic_baseline_favorite_24);
                        } else {
                            ivLike.setImageResource(R.drawable.ic_baseline_favorite_red_24);
                        }
                        MyApplication.show("点赞成功");
                    }

                    @Override
                    public void error(String msg) {

                    }
                });
            }
        });
    }

    private void setAdapter() {
        fastFoodDetailAdapter = new FastFoodDetailAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rvList.setAdapter(fastFoodDetailAdapter);
        rvList.setLayoutManager(linearLayoutManager);
    }

    private void initData() {
        Call<ApiResp> call = Api.api().get(MyApplication.TOKEN, "/prod-api/api/kitchen-helper/dishes/" + id);
        Api.send(call, FastFoodDetailBean.class, new CallBack<FastFoodDetailBean>() {
            @Override
            public void success(FastFoodDetailBean data) {
                fastFoodDetailAdapter.setData(data.getData().getStepList());
                tvName.setText(data.getData().getName());
                tvIngredients.setText(data.getData().getIngredients());
                rows = data.getData();
                if (rows.isMyCollectionState()) {
                    ivCollect.setImageResource(R.drawable.ic_baseline_grade_yellow_24);
                }
                if (rows.isMyLikeState()) {
                    ivLike.setImageResource(R.drawable.ic_baseline_favorite_red_24);
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    @Override
    public String getMyTitle() {
        return "菜品详情";
    }

    private void initView() {
        tvName = findViewById(R.id.tv_name);
        ivLike = findViewById(R.id.iv_like);
        ivCollect = findViewById(R.id.iv_collect);
        tvIngredients = findViewById(R.id.tv_ingredients);
        rvList = findViewById(R.id.rv_list);
    }
}